﻿using System;
using System.Linq;
using System.Windows.Forms;
using AppAcc.Models;

namespace AppAcc.Views.DanhMuc
{
    public partial class FormKhoHangThem : DevComponents.DotNetBar.RibbonForm
    {
        //private AppAccContext db = new AppAccContext();
        private PhanMenKeToanEntities1 db = new PhanMenKeToanEntities1();
        public FormKhoHangThem()
        {
            InitializeComponent();
        }

        private void FormKhoHangThem_Load(object sender, EventArgs e)
        {
            GetLoaiKho();
        }

        private void txtTenKhoHang_TextChanged(object sender, EventArgs e)
        {
            KiemTra();
        }

        private void txtDiaChi_TextChanged(object sender, EventArgs e)
        {
            KiemTra();
        }

        private void cbbLoaiKhoHang_SelectedIndexChanged(object sender, EventArgs e)
        {
            KiemTra();
        }

        private void bttLuu_Click(object sender, EventArgs e)
        {
            Create();
        }

        private void bttThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        void Create()
        {
            try
            {
                var t = new KhoHang();
                t.TenKhoHang = txtTenKhoHang.Text;
                t.DiaChi = txtDiaChi.Text;
                t.DienGiai = txtDienGiai.Text;
                t.LoaiKhoHangId = Convert.ToInt32(cbbLoaiKhoHang.SelectedValue);
                db.KhoHangs.Add(t);
                db.SaveChanges();
                Reset();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        void KiemTra()
        {
            if (txtDiaChi.Text != "" && txtTenKhoHang.Text != "" && Convert.ToInt32(cbbLoaiKhoHang.SelectedValue) != 0)
            {
                bttLuu.Enabled = true;
            }
            else
            {
                bttLuu.Enabled = false;
            }
        }

        void Reset()
        {
            txtDiaChi.Text = "";
            txtDienGiai.Text = "";
            txtTenKhoHang.Text = "";
            KiemTra();
        }

        void GetLoaiKho()
        {
            cbbLoaiKhoHang.DataSource = db.LoaiKhoHangs.ToList();
            cbbLoaiKhoHang.DisplayMember = "TenLoaiKho";
            cbbLoaiKhoHang.ValueMember = "Id";
            cbbLoaiKhoHang.DropDownColumns = "ID,TenLoaiKhoHang";
            cbbLoaiKhoHang.DropDownColumnsHeaders = "Mã loại\r\nTên loại kho hàng\r\n";
        }
    }
}
